package com.softtown.modules.dapai.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softtown.common.service.impl.BaseServiceImpl;
import com.softtown.common.utils.ConvertUtils;
import com.softtown.modules.dapai.dao.CompOutboundOrderDao;
import com.softtown.modules.dapai.dto.*;
import com.softtown.modules.dapai.entity.*;
import com.softtown.modules.dapai.service.*;
import com.softtown.modules.dapai.vo.CompLeaseOrderVO;
import com.softtown.modules.dapai.vo.CompSaleOrderVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
* @Author: Ding Ya Yong
* @Description: 售后申请单
* @date: 2022/1/2 11:38
* @Param:
* @return:
*/

@Service
public class CompOutboundOrderServiceImpl extends
        BaseServiceImpl<CompOutboundOrderDao, CompOutboundOrderEntity> implements CompOutboundOrderService {


    @Resource
    private CompOutboundOrderDetailService compOutboundOrderDetailService;

    @Resource
    private CompSaleOrderService compSaleOrderService;

    @Resource
    private CompLeaseOrderService compLeaseOrderService;

    @Resource
    private CompSaleDeliveryRecordsService compSaleDeliveryRecordsService;

    @Resource
    private CompLeaseRecordsService compLeaseRecordsService;


    @Override
    public PageInfo<CompOutboundOrderDTO> getOutboundOrderPageList(CompOutboundOrderDTO compOutboundOrderDTO) {
        Integer pageSize = compOutboundOrderDTO.getPageSize();
        Integer page = compOutboundOrderDTO.getPage();
        if (pageSize > 0 && page > 0) {
            PageHelper.startPage(page, pageSize);
        }
        List<CompOutboundOrderDTO> compOutboundOrderDTOList =
                baseDao.getOutboundOrderPageList(compOutboundOrderDTO.getCompId(), compOutboundOrderDTO.getParam());
        return  new PageInfo<>(compOutboundOrderDTOList);
    }

    @Override
    public PageInfo<CompOutboundOrderDetailDTO> getOutboundOrderDetailPageList(CompOutboundOrderDetailDTO compOutboundOrderDetailDTO) {
        Integer pageSize = compOutboundOrderDetailDTO.getPageSize();
        Integer page = compOutboundOrderDetailDTO.getPage();
        if (pageSize > 0 && page > 0) {
            PageHelper.startPage(page, pageSize);
        }
        List<CompOutboundOrderDetailDTO> compOutboundOrderDTOList =
                baseDao.getOutboundOrderDetailPageList(compOutboundOrderDetailDTO.getCompId(), compOutboundOrderDetailDTO.getParam());
        return  new PageInfo<>(compOutboundOrderDTOList);
    }

    @Override
    public CompOutboundOrderAddDTO getOutboundOrderById(Long compId, Long id) {
        CompOutboundOrderAddDTO compOutboundOrderAddDTO = new CompOutboundOrderAddDTO();
        compOutboundOrderAddDTO.setCompOutboundOrderEntity(baseDao.getOutboundOrderById(compId, id));
        compOutboundOrderAddDTO.setCompOutboundOrderDetailEntityList(baseDao.getOutboundOrderDetailList(compId, id));
        return compOutboundOrderAddDTO;
    }

    @Override
    @Transactional
    public void addOutboundOrder(CompOutboundOrderAddDTO CompOutboundOrderAddDTO) {
        CompOutboundOrderEntity compOutboundOrderEntity =ConvertUtils
                .sourceToTarget(CompOutboundOrderAddDTO.getCompOutboundOrderEntity(), CompOutboundOrderEntity.class);
        insert(compOutboundOrderEntity);
        CompOutboundOrderAddDTO.getCompOutboundOrderDetailEntityList().forEach(item ->{
            item.setOutboundOrderId(compOutboundOrderEntity.getId());
            item.setCompId(compOutboundOrderEntity.getCompId());
//            if (compOutboundOrderEntity.getOrderType() == 0){
//                CompSaleDeliveryRecordsEntity compSaleDeliveryRecordsEntity = new CompSaleDeliveryRecordsEntity();
//                compSaleDeliveryRecordsEntity.setId(compOutboundOrderEntity.getId());
//                compSaleDeliveryRecordsEntity.setCompId(compOutboundOrderEntity.getCompId());
//                compSaleDeliveryRecordsEntity.setMacId(item.getMacId());
//                compSaleDeliveryRecordsEntity.setSaleOrderId(compOutboundOrderEntity.getSaleOrderId());
//                CompSaleOrderVO compSaleOrderVO = compSaleOrderService.getSaleOrderById(compOutboundOrderEntity.getCompId(), compOutboundOrderEntity.getSaleOrderId());
//                compSaleOrderVO.getCompSaleOrderDetailEntities().forEach(item1 ->{
//                    if (item.getMacId() == item1.getMacScrewId()){
//                        compSaleDeliveryRecordsEntity.setNum(item1.getNum());
//                    }
//                });
//                compSaleDeliveryRecordsEntity.setIssuedQuantity(item.getIssuedQuantity());
//                compSaleDeliveryRecordsEntity.setUndeliveredQuantity(item.getOutboundQuantity() - item.getIssuedQuantity());
//                compSaleDeliveryRecordsEntity.setCreator(item.getCreator());
//                compSaleDeliveryRecordsEntity.setCreateDate(item.getCreateDate());
//                compSaleDeliveryRecordsEntity.setUpdater(item.getUpdater());
//                compSaleDeliveryRecordsEntity.setUpdateDate(item.getUpdateDate());
//                compSaleDeliveryRecordsEntity.setUpdateTimestamp(item.getUpdateTimestamp());
//                compSaleDeliveryRecordsService.insert(compSaleDeliveryRecordsEntity);
//            }else if(compOutboundOrderEntity.getOrderType() == 1){
//                CompLeaseRecordsEntity compLeaseRecordsEntity = new CompLeaseRecordsEntity();
//                compLeaseRecordsEntity.setId(compOutboundOrderEntity.getId());
//                compLeaseRecordsEntity.setCompId(compOutboundOrderEntity.getCompId());
//                compLeaseRecordsEntity.setMacId(item.getMacId());
//                compLeaseRecordsEntity.setLeaseOrderId(compOutboundOrderEntity.getLeaseOrderId());
//                CompLeaseOrderVO compLeaseOrderVO = compLeaseOrderService.getLeaseOrderById(compOutboundOrderEntity.getCompId(), compOutboundOrderEntity.getLeaseOrderId());
//                compLeaseOrderVO.getCompLeaseOrderDetailEntities().forEach(item1 ->{
//                    if (item.getMacId() == item1.getMacScrewId()){
//                        compLeaseRecordsEntity.setNum(item1.getNum());
//                        compLeaseRecordsEntity.setEndDate(item1.getEndDate());
//                        compLeaseRecordsEntity.setTotalTime(item1.getTotalTime());
//                    }
//                });
//                compLeaseRecordsEntity.setCreator(item.getCreator());
//                compLeaseRecordsEntity.setCreateDate(item.getCreateDate());
//                compLeaseRecordsEntity.setUpdater(item.getUpdater());
//                compLeaseRecordsEntity.setUpdateDate(item.getUpdateDate());
//                compLeaseRecordsEntity.setUpdateTimestamp(item.getUpdateTimestamp());
//                compLeaseRecordsService.insert(compLeaseRecordsEntity);
//            }
        });
        compOutboundOrderDetailService.insertBatch(ConvertUtils
                .sourceToTarget(CompOutboundOrderAddDTO.getCompOutboundOrderDetailEntityList(), CompOutboundOrderDetailEntity.class));

    }

    @Override
    @Transactional
    public void updateMacStock(CompMacEntity compMacEntity) {
        baseDao.updateMacStock(compMacEntity.getCompId(), compMacEntity.getId(), compMacEntity.getStock());
    }

    @Override
    @Transactional
    public void updateOutboundOrder(CompOutboundOrderAddDTO CompOutboundOrderAddDTO) {
        CompOutboundOrderEntity compOutboundOrderEntity =ConvertUtils
                .sourceToTarget(CompOutboundOrderAddDTO.getCompOutboundOrderEntity(), CompOutboundOrderEntity.class);
        updateById(compOutboundOrderEntity);
        baseDao.deleteDetail(compOutboundOrderEntity.getCompId(), compOutboundOrderEntity.getId());
        CompOutboundOrderAddDTO.getCompOutboundOrderDetailEntityList().forEach(item ->{
            item.setOutboundOrderId(compOutboundOrderEntity.getId());
            item.setCompId(compOutboundOrderEntity.getCompId());
        });
        compOutboundOrderDetailService.insertBatch(ConvertUtils
                .sourceToTarget(CompOutboundOrderAddDTO.getCompOutboundOrderDetailEntityList(), CompOutboundOrderDetailEntity.class));
    }

    @Override
    public void deleteOutboundOrder(CompOutboundOrderDTO compOutboundOrderDTO) {
        baseDao.deleteOutboundOrder(compOutboundOrderDTO.getCompId(), compOutboundOrderDTO.getId());
        baseDao.deleteDetail(compOutboundOrderDTO.getCompId(), compOutboundOrderDTO.getId());
    }
}
